package com.test;

import com.helper.Debug;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.luaj.vm2.ast.Str;

import java.io.*;
import java.util.Date;

public abstract class WorkThread extends Thread {
    public int id;
    public int times;

    @Override
    public synchronized void start() {
        super.start();

    }

    public static String mainPath = "F:\\JAVAProj\\MServer\\log\\";
    public void write(String value, String name) throws IOException {
        File newlog = new File(mainPath + name+".txt");
        if(!newlog.isFile()){
            newlog.createNewFile();
        }
        FileWriter fw =   new FileWriter(newlog,true);
        fw.write(value+"\r\n");
        fw.close();
    }
    public Date date;
    public WorkThread(int id){
        date = new Date();
        rTime = 0;
        this.id = id;

    }
    public long rTime;
    public int update(){

        rTime = new Date().getTime() - date.getTime();
        return (int)rTime;
    }
    public abstract void doSomething() throws IOException;
    public abstract int runTime();

    @Override
    public void run() {
        date = new Date();
        Debug.log("任务："+id+"开启");
        while (rTime<runTime()){
            try {
                doSomething();
            } catch (IOException e) {
                e.printStackTrace();
            }
            times++;
            update();
        }
        Debug.log("任务："+id+"结束");

    }
}
